package com.sxran.im.handler;

import com.google.common.collect.Lists;
import com.sxran.im.bean.AcceptFriendApplicationHttpResp;
import com.sxran.im.bean.AcceptGroupApplicationHttpResp;
import com.sxran.im.controller.ImController;
import com.sxran.im.dao.ImFriendMapper;
import com.sxran.im.dao.ImUserGroupMapper;
import com.sxran.im.info.*;
import com.sxran.im.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class AcceptGroupApplicationCmdHandler extends RefuseGroupApplicationCmdHandler{
    @Resource
    protected UserService userService;
    @Resource
    private ImFriendMapper imFriendMapper;

    @Resource
    private ImUserGroupMapper imUserGroupMapper;

    @Override
    public Object doBeanService(ImGroupInvite json) {
        // 修改申请,通知的状态。
        this.updateState(json,100);
        //
        AcceptGroupApplicationHttpResp acceptFriendApplicationHttpResp = new AcceptGroupApplicationHttpResp();
        acceptFriendApplicationHttpResp.setSysMeg("新成员加入群聊");
        acceptFriendApplicationHttpResp.setHelloMeg("欢迎加入群聊");
        // 设置最新的用户信息。
        String fromUid = json.getUserId(); //json.getFromUid();
        // 仅仅通知进入群聊就行啦。 把新用户绑定到新群聊中。
        acceptFriendApplicationHttpResp.setBindGroupUsers(Lists.newArrayList(fromUid));
//        System.out.println("添加内容:"+ curUserId + "|" +fromUid );
        ImUser imUser = userService.selectByUserId(fromUid);
        acceptFriendApplicationHttpResp.setApplicationUserInfo(imUser);
        // 添加群聊记录。
        ImUserGroup imUserGroup = new ImUserGroup();
        String groupId = json.getGroupId();
        acceptFriendApplicationHttpResp.setGroupId(groupId);
        imUserGroup.setGid(groupId);
        imUserGroup.setUid(fromUid);
        imUserGroup.setCreate_time(new Date());
        imUserGroup.setRole("user");
        imUserGroupMapper.insert(imUserGroup);
        return acceptFriendApplicationHttpResp;
    }

}
